<template>
  <el-card class="box-card">
    <div style="line-height: 0px">
      <el-form :inline="true" class="demo-form-inline" size="small">
        <el-form-item>
          <el-input
            v-model="formInline.operation"
            clearable
            placeholder="操作名称"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="queryList()">查询</el-button>
        </el-form-item>

        <el-form-item>
          <el-button
            type="danger" 
            @click="deleteList()"
            :disabled="multipleSelection.length === 0"
            >删除</el-button
          >
        </el-form-item>

      </el-form>
      <el-table
        stripe
        :data="logList"
        style="width: 100%"
        @selection-change="handleSelectionChange"
        :cell-style="{'text-align':'center'}"
        :header-cell-style="{'text-align':'center'}"
      >
        <el-table-column class="table-selection" type="selection" width="60">
        </el-table-column>
        <el-table-column prop="id" label="id" width="40">
        </el-table-column>
        <el-table-column prop="username" label="用户" width="100">
        </el-table-column>
        <el-table-column prop="operation" label="操作" width="110">
        </el-table-column>
        <el-table-column prop="method" label="方法" width="220">
        </el-table-column>
        <el-table-column prop="params" label="参数" width="220">
        </el-table-column>
        <el-table-column prop="time" label="执行时间(ms)" width="120">
        </el-table-column>
        <el-table-column prop="ip" label="ip" width="150">
        </el-table-column>
        <el-table-column prop="createDate" label="日期" width="200">
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="this.pageIndex"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="this.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="this.total"
        >
        </el-pagination>
      </div>
    </div>
  </el-card>
</template>

<script>
export default {
  name: "SysLog",
  data() {
    return {
      rowData: {
        id: "",
        userName: "",
        operation: "",
        method: "",
        params: "",
        time: "",
        ip: "",
        createDate: "",
      },
      multipleSelection: [],
      formInline: {
        operation: "",
      },
      logList: [],
      pageIndex: 1,
      pageSize: 10,
      total: 0,
    };
  },
  methods: {
    deleteList() {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$http
            .post("/sys/sysLog/deleteList", this.multipleSelection)
            .then((res) => {
              if (res.data.data > 0) {
                this.$message({
                  type: "success",
                  message: "删除成功!共删除" + res.data.data + "条记录",
                });
                this.queryList();
              } else {
                this.$message({
                  type: "warning",
                  message: "系统繁忙!",
                });
              }
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    handleSelectionChange(val) {
      this.multipleSelection = [];
      val.forEach((element) => {
        this.multipleSelection.push(element.id);
      });
    },
    handleDelete(index, row) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$http
            .get("/sys/sysLog/deleteOne?id=" + row.id)
            .then((res) => {
              
              if (res.data.data) {
                this.$message({
                  type: "success",
                  message: "删除成功!",
                });
                this.queryList();
              } else {
                this.$message({
                  type: "warning",
                  message: "系统繁忙!",
                });
              }
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.queryList();
    },
    handleCurrentChange(val) {
      this.pageIndex = val;
      this.queryList();
    },
    queryList() {
      const params = {
        params: {
          pageIndex: this.pageIndex,
          pageSize: this.pageSize,
          operation: this.formInline.operation,
        },
      };
      this.$http.get("/sys/sysLog/queryList", params).then((res) => {
        this.logList = res.data.data.list;
        this.total = res.data.data.totalCount;
      });
    },
  },
  mounted() {
    this.queryList();
  },
};
</script>

<style lang="less" scoped>
.form-input {
  margin-left: -100px;
}
/deep/ .el-form-item__error {
  margin-left: -100px;
}
/deep/ .cell {
  padding-left: 10px;
}
</style>